﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="/Scripts/jquery-3.3.1.js"></script>
    <script src="/Scripts/Base64/core-min.js"></script>
    <script src="/Scripts/Base64/enc-base64-min.js"></script>
    <script>
        $(function () {
            //是否登录
            var UserToken = localStorage.getItem("UserToken");
            if (UserToken != null && UserToken != "") {
                var payload = UserToken.split('.')[1];
                //编码
                //var str = CryptoJS.enc.Utf8.parse($("#source").val());
                //var base64 = CryptoJS.enc.Base64.stringify(str);

                //解码
                var jwtDataWords = CryptoJS.enc.Base64.parse(payload);
                var jwtDataStr = jwtDataWords.toString(CryptoJS.enc.Utf8);
                var jwtData = JSON.parse(jwtDataStr);

                var html = `<span id="user">${jwtData.customData.name}<a href="javascript:void(0);" onclick="Exit()">退出</a></span>`;
                $("#user").html(html);
                $("#div_test").html(`<h3>本地无处不在的token：</h3>${jwtDataStr}`);
            }

            //请求信息2
            $.ajax({
                type: "get",
                url: "http://localhost:58975/api/Home/GetHome",
                data: {},
                beforeSend: function (XHR) {
                    XHR.setRequestHeader('User-Token', UserToken);
                },
                success: function (result) {
                    var html = `<h3>请求的信息：</h3>  id : ${result.Data.id},name：${result.Data.name},Message:${result.Message}`;
                    $("#div_test2").html(html);
                },
                error: function (respon) {
                    $("#div_test2").html("无权限访问");
                },
                complete: function (respon) {
                    if (respon.status == "401") {
                        //利用对话框返回的值
                        if (confirm("没有登录，是否要登录")) {
                            window.location = "/User/Login.html";
                        }
                        else {

                        }
                    }
                }
            });

            //请求信息3
            $.ajax({
                type: "get",
                url: "http://localhost:58975/api/Home/KownWho",
                data: {},
                beforeSend: function (XHR) {
                    XHR.setRequestHeader('User-Token', UserToken);
                },
                success: function (data) {
                    var html = `<h3>请求的信息2：api端知道你是谁</h3> ${JSON.stringify(data.Data)} `;
                    $("#div_test3").html(html);
                },
                error: function (respon) {
                    $("#div_test3").html("无权限访问");
                },
                complete: function (respon) {
                    if (respon.status == "401") {
                        //利用对话框返回的值
                        if (confirm("没有登录，是否要登录")) {
                            window.location = "/User/Login.html";
                        }
                        else {

                        }
                    }
                }
            });
        });

        function Exit() {
            localStorage.clear();
            window.location = "/";
        }
    </script>
</head>
<body>
    欢迎界面。<span id="user"><a href="/User/Login.html">登录</a></span>
    <div id="div_test"></div>
    <div id="div_test2"></div>
    <div id="div_test3"></div>
</body>
</html>